package com.yh.signinmongodb.controller;

import com.yh.signinmongodb.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/student")
    public String student(){
        return "student";
    }

    @GetMapping("/signIn")
    public String signIn(String name){
        studentService.updateStudentByStats(name);
        return "student";
    }

    @GetMapping("/")
    public String list(Model model){
        model.addAttribute("studentList",studentService.selectStudent());
        return "teacher";
    }

    @GetMapping("/updateState")
    public String updateState(){
        studentService.updateAllStats();
        return "redirect:/";
    }

    @GetMapping("/importView")
    public String uploadView(){
        return "upload";
    }

    @PostMapping("/import")
    public String upload(MultipartFile file, Model model){
        String fileName = file.getOriginalFilename();
        try {
            studentService.upload(fileName,file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("state",studentService.selectStudent());
        return "redirect:/";
    }

}
